package com.example.forum2.controller;

import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.exception.ApplicationException;
import com.example.forum2.model.Board;
import com.example.forum2.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "版块接口")
@RequestMapping("/board")
public class BoardController {
//    接下来完成controller层的代码,默认值是9
    @Value("${forum.index.board-num:9}")
    private Integer defaultNum;
    @Resource
    private IBoardService iBoardService;
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为:"+ defaultNum);
        List<Board> list=iBoardService.selectByNum(defaultNum);
//        判断List是否为空
        if(list==null){
            list=new ArrayList<>();
        }
        return AppResult.success(list);
    }

//    根据前端传入的id,来获取前端的信息
    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@ApiParam("板块id") @RequestParam("id") @NonNull Long id){
//      非空校验已经完成了,接下来调用service层的代码
        Board board=iBoardService.selectById(id);
        if(board.getDeleteState()==1 || board==null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        return AppResult.success(board);
    }



}
